package com.example.forum.controller;

import com.example.forum.annotation.LoginRequired;
import com.example.forum.domain.Comment;
import com.example.forum.domain.DiscussPost;
import com.example.forum.domain.Page;
import com.example.forum.domain.User;
import com.example.forum.service.CommentService;
import com.example.forum.service.DiscussPostService;
import com.example.forum.service.UserService;
import com.example.forum.utils.HostHolder;
import com.example.forum.utils.SimpleUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class DiscussPostController {
    @Autowired
    private DiscussPostService discussPostService;
    @Autowired
    private CommentService commentService;
    @Autowired
    private UserService userService;
    @Autowired
    private HostHolder hostHolder;

    @RequestMapping("/addDiscussPost")
    @ResponseBody
    public String addDiscussPost(String title,String text){
        User user = hostHolder.getUser();
        if(user==null){
            return SimpleUtils.toJson(1,"发布失败,请先登录");
        }
        //将数据添加到数据库
        try {
            discussPostService.addDiscussPost(title,text);
        }catch (Exception e){
            return SimpleUtils.toJson(1,"发布失败,遇到未知错误");
        }
        return SimpleUtils.toJson(0,"发布成功");
    }

    @RequestMapping("/discussPostInfo")
    public String discussPostInfo(int id,Model model, @RequestParam(value = "curPage",required = false,defaultValue = "1")int curPage,
                                  @RequestParam(value = "pageSize",required = false,defaultValue = "5")int pageSize){
        //查询帖子
        DiscussPost discussPost = discussPostService.findById(id);
        User user = userService.findById(Integer.parseInt(discussPost.getUserId()));
        model.addAttribute("discussPost",discussPost);
        model.addAttribute("user",user);
        //查询帖子的评论
        List<Comment> commentList = commentService.findByEntityType
                (1, discussPost.getId(), (curPage - 1) * pageSize, pageSize);
        //查询帖子的评论的条数
        int totalCount=commentService.findByEntityTypeCount(1,discussPost.getId());
        List<Map<String,Object>> commentVoList=new ArrayList<>();
        if(commentList!=null){
            for(Comment comment:commentList){
                Map<String,Object> map=new HashMap<>();
                //这个评论的用户
                map.put("user",userService.findById(comment.getUserId()));
                //这个评论
                map.put("comment",comment);
                List<Map<String,Object>> replyVoList=new ArrayList<>();
                //这个评论的所有回复(回复列表)
                List<Comment> replyList = commentService.findByEntityType(2, comment.getId(),0,Integer.MAX_VALUE );
                if(replyList!=null){
                    for(Comment reply:replyList){
                        Map<String,Object> map1=new HashMap<>();
                        //这个评论一条的回复
                        map1.put("reply",reply);
                        //这个评论一条回复的用户
                        map1.put("user",userService.findById(reply.getUserId()));
                        //回复的对象
                        User target=reply.getTargetId()==0?null:userService.findById(reply.getTargetId());
                        map1.put("target",target);
                        replyVoList.add(map1);
                    }
                }
                //这个评论的回复
                map.put("replyVo",replyVoList);
                //这个评论的回复数量
                Integer replyCount = commentService.findByEntityTypeCount(2, comment.getId());
                map.put("replyCount",replyCount);
                commentVoList.add(map);
            }
        }
        model.addAttribute("commentVo",commentVoList);
        Page page=new Page();
        page.setCurPage(curPage);
        page.setPageSize(pageSize);
        page.setTotalCount(totalCount);
        model.addAttribute("page",page);
        return "site/discuss-detail";
    }
}
